package com.yami.shop.cp.comment.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.UserAddr;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.IdUtil;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.cp.comment.dao.CollaborativePurchaseAddrMapper;
import com.yami.shop.cp.comment.dao.CollaborativePurchaseMapper;
import com.yami.shop.cp.comment.dao.CollaborativeSubmitPersonMapper;
import com.yami.shop.cp.comment.dto.CPOrderDto;
import com.yami.shop.cp.comment.dto.CPSubmitPersonCount;
import com.yami.shop.cp.comment.dto.CPSubmitPersonInfoAndSubmitStatus;
import com.yami.shop.cp.comment.dto.CollaborativePurchaseMergerDto;
import com.yami.shop.cp.comment.enums.CPSubmitPersonSubmitStatusEnum;
import com.yami.shop.cp.comment.model.CollaborativePurchase;
import com.yami.shop.cp.comment.model.CollaborativePurchaseAddr;
import com.yami.shop.cp.comment.model.CollaborativeSubmitPerson;
import com.yami.shop.cp.comment.model.UserSubmitPerson;
import com.yami.shop.cp.comment.param.CPSearchParam;
import com.yami.shop.cp.comment.service.CollaborativePurchaseService;
import com.yami.shop.dao.UserAddrMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xch
 * @since 2021-06-23
 */
@Service
@AllArgsConstructor
public class CollaborativePurchaseServiceImpl extends ServiceImpl<CollaborativePurchaseMapper, CollaborativePurchase> implements CollaborativePurchaseService {

    private final CollaborativePurchaseMapper collaborativePurchaseMapper;

    private final CollaborativePurchaseAddrMapper collaborativePurchaseAddrMapper;

    private final CollaborativeSubmitPersonMapper collaborativeSubmitPersonMapper;

    private final UserAddrMapper userAddrMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createCollaborativePurchase(CollaborativePurchaseMergerDto collaborativePurchaseMergerDto) {

        CollaborativePurchase collaborativePurchase = CollaborativePurchase.getCollaborativePurchase(collaborativePurchaseMergerDto);
        collaborativePurchase.setId(IdUtil.dateTimeId("XT"));
        collaborativePurchaseMapper.insert(collaborativePurchase);

        CollaborativePurchaseAddr collaborativePurchaseAddr = CollaborativePurchaseAddr.getCollaborativePurchaseAddr(collaborativePurchaseMergerDto);
        collaborativePurchaseAddr.setCollaborativePurchaseId(collaborativePurchase.getId());
        // 获取用户默认地址
        UserAddr userAddr = userAddrMapper.getDefaultUserAddr(collaborativePurchaseMergerDto.getUserId());
        collaborativePurchaseAddr.setReceiver(userAddr.getReceiver());
        collaborativePurchaseAddr.setMobile(userAddr.getMobile());

        collaborativePurchaseAddrMapper.insert(collaborativePurchaseAddr);
        // 暂时先for循环添加，后面改为，数据库的foreach或者是扩展一下mapper添加上批处理
        for (UserSubmitPerson submitPerson : collaborativePurchaseMergerDto.getSubmitPersons()) {
            CollaborativeSubmitPerson collaborativeSubmitPerson = new CollaborativeSubmitPerson();
            collaborativeSubmitPerson.setUserSubmitPersonId(submitPerson.getId());
            collaborativeSubmitPerson.setCollaborativePurchaseId(collaborativePurchase.getId());
            collaborativeSubmitPersonMapper.insert(collaborativeSubmitPerson);
        }
    }

    @Override
    public CollaborativePurchaseMergerDto queryCollaborativePurchaseMergerDto(String userId, String collaborativePurchaseId) {

        return collaborativePurchaseMapper.queryCollaborativePurchaseMergerDto(userId, collaborativePurchaseId);
    }

    @Override
    public void updateCollaborativePurchase(CollaborativePurchaseMergerDto collaborativePurchaseMergerDto) {
        CollaborativePurchase collaborativePurchase = CollaborativePurchase.getCollaborativePurchase(collaborativePurchaseMergerDto);
        CollaborativePurchase collaborativePurchaseOld = collaborativePurchaseMapper.selectById(collaborativePurchase.getId());
        if (Objects.isNull(collaborativePurchaseOld)) {
            throw new YamiShopBindException("yami.collaborative.purchase.update.error");
        }
        collaborativePurchaseMapper.updateById(collaborativePurchase);

        CollaborativePurchaseAddr collaborativePurchaseAddr = CollaborativePurchaseAddr.getCollaborativePurchaseAddr(collaborativePurchaseMergerDto);
        collaborativePurchaseAddr.setCollaborativePurchaseId(collaborativePurchase.getId());

        collaborativePurchaseAddrMapper.update(collaborativePurchaseAddr, new LambdaQueryWrapper<CollaborativePurchaseAddr>()
                .eq(CollaborativePurchaseAddr::getCollaborativePurchaseId, collaborativePurchase.getId()));

        collaborativeSubmitPersonMapper.delete(new LambdaQueryWrapper<CollaborativeSubmitPerson>()
                .eq(CollaborativeSubmitPerson::getCollaborativePurchaseId, collaborativePurchase.getId()));

        // 暂时先for循环添加，后面改为，数据库的foreach或者是扩展一下mapper添加上批处理
        for (UserSubmitPerson submitPerson : collaborativePurchaseMergerDto.getSubmitPersons()) {
            CollaborativeSubmitPerson collaborativeSubmitPerson = new CollaborativeSubmitPerson();
            collaborativeSubmitPerson.setUserSubmitPersonId(submitPerson.getId());
            collaborativeSubmitPerson.setCollaborativePurchaseId(collaborativePurchase.getId());
            collaborativeSubmitPersonMapper.insert(collaborativeSubmitPerson);
        }
    }

    @Override
    public IPage<CollaborativePurchase> pageByUserIdAndStatus(PageParam page, String userId, String status) {
        return collaborativePurchaseMapper.pageByUserIdAndStatus(page, userId, status);
    }

    @Override
    public IPage<CollaborativePurchase> pageBySubmitPersonIdAndStatus(PageParam page, String mobile, String status) {
        return collaborativePurchaseMapper.pageBySubmitPersonIdAndStatus(page, mobile, status);
    }

    @Override
    public CPSubmitPersonCount getCPSubmitPersonCount(String collaborativePurchaseId) {
        List<CPSubmitPersonInfoAndSubmitStatus> cpSubmitPersonInfoAndSubmitStatuses = collaborativePurchaseMapper.getCPSubmitPersonInfoAndSubmitStatusById(collaborativePurchaseId);
        CPSubmitPersonCount cpSubmitPersonCount = new CPSubmitPersonCount();
        for (CPSubmitPersonInfoAndSubmitStatus cpSubmitPersonInfoAndSubmitStatus : cpSubmitPersonInfoAndSubmitStatuses) {
            if (Objects.equals(CPSubmitPersonSubmitStatusEnum.NOT_SUBMIT.getValue(), cpSubmitPersonInfoAndSubmitStatus.getStatus())) {
                cpSubmitPersonCount.setNotSubmitCount(cpSubmitPersonCount.getNotSubmitCount() + 1);
            }
            if (Objects.equals(CPSubmitPersonSubmitStatusEnum.SUBMIT.getValue(), cpSubmitPersonInfoAndSubmitStatus.getStatus())) {
                cpSubmitPersonCount.setSubmitCount(cpSubmitPersonCount.getSubmitCount() + 1);
            }
        }
        cpSubmitPersonCount.setCpSubmitPersonInfoAndSubmitStatusList(cpSubmitPersonInfoAndSubmitStatuses);
        return cpSubmitPersonCount;
    }

    @Override
    public IPage<CollaborativePurchase> getSearchCP(PageParam page, CPSearchParam cpSearchParam) {
        return collaborativePurchaseMapper.getSearchCP(page,cpSearchParam);
    }

    @Override
    public List<CollaborativePurchase> getSearchCP( CPSearchParam cpSearchParam) {
        return collaborativePurchaseMapper.getSearchCP(cpSearchParam);
    }

    @Override
    public List<CPOrderDto> getCPOrderNumById(String cpId) {
        return collaborativePurchaseMapper.getCPOrderNumById(cpId);
    }
}
